<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;

/**
 * 学生
 */
class Student extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (学生信息)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/17)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/student/profile)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="班级ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579241875",
    "data": {
    "id": 2369,
    "number": "20191008",
    "edu_id_number": "1008",
    "card_number": "55AA2254645644",
    "id_type": null,
    "id_number": null,
    "name": "薛凯琦",
    "pinyin": "wangxiaoer",
    "gender": 0,
    "birthday": "2005-03-12",
    "enrollment_type": "10015",
    "enrollment_year": "10015",
    "school_id": 36,
    "grade_id": 1,
    "class_id": 52,
    "status": 1,
    "created_at": 1574066510,
    "avatar": "http://mp.uxuj.test/uploads/20191120/9100de52592d359342e1d4a4e117e7ad.jpg",
    "parents_id": 112,
    "nationality_id": null,
    "nation_id": null,
    "political_appearance_id": null,
    "address_id": null,
    "is_bed": 0,
    "class_name": "1班",
    "grade_name": "一年级",
    "headmaster_name": "test"
    }
    })
     */
    public function profile()
    {
        $id = $this->request->request('id', 0);
        if (!$id) {
            $this->error('参数有误');
        }

        $result = \app\common\model\Student::alias('student')
            ->join('__CLASSES__ classes', 'classes.id = student.class_id')
            ->join('__GRADE__ grade', 'grade.id = student.grade_id')
            ->join('__SCHOOL__ school', 'school.id = student.school_id')
            ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
            ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
            ->where('student.id', $id)
            ->field('student.*,school.name school_name,classes.name class_name,grade.name grade_name,classes.headmaster headmaster_id,user.nickname headmaster_name')
            ->find();

        if ($result && !strstr($result->avatar, 'http') && strstr($result->avatar, '/uploads/')) {
            $result->avatar = config('app_url') . $result->avatar;
        }

        $result->gender_text = User::getGender($result->gender);

        $this->success('', $result);

    }

    public function profiles()
    {
        $ids = $this->request->request('ids/a', 0);

        $result = \app\common\model\Student::alias('student')
            ->join('__CLASSES__ classes', 'classes.id = student.class_id')
            ->join('__GRADE__ grade', 'grade.id = student.grade_id')
            ->join('__SCHOOL__ school', 'school.id = student.school_id')
            ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
            ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
            ->whereIn('student.id', $ids)
            ->field('student.*,classes.name class_name,grade.name grade_name,classes.headmaster headmaster_id,user.nickname headmaster_name')
            ->column('student.*,school.name school_name,classes.name class_name,grade.name grade_name,classes.headmaster headmaster_id,user.nickname headmaster_name', 'student.id');

        foreach ($result as $key => $value) {
            if (!strstr($value['avatar'], 'http') && strstr($value['avatar'], '/uploads/')) {
                $result[$key]['avatar'] = config('app_url') . $value['avatar'];
            }
        }

        $this->success('', $result);
    }

    /**
     * @ApiTitle    (学生列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/2/18)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/student/lists)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="grade_id", type="int", required=true, description="年级 id", sample="")
     * @ApiParams   (name="class_id", type="int", required=false, description="班级 id", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function lists()
    {
        $grade_id = $this->request->request('grade_id', 0);
        $class_id = $this->request->request('class_id', 0);
        $keywords = $this->request->request('keywords', '');

        if ($class_id) {
            $list = \app\common\model\Student::alias('student')
                ->join('__CLASSES__ classes', 'classes.id = student.class_id')
                ->where('student.class_id', $class_id)
                ->where('student.grade_id', $grade_id)
                ->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('student.name|student.number', 'like', "%$keywords%");
                    }
                })
                ->where('student.school_id', $this->auth->school_id)
                ->where('student.status', \app\common\model\Student::DISPLAY)
                ->field('student.id,student.number,student.card_number,student.name,student.avatar,student.pinyin,student.gender,student.birthday,student.school_id,student.grade_id,student.class_id,student.created_at,classes.name class_name')
                ->paginate();
        } else {
            $list = \app\common\model\Student::alias('student')
                ->join('__CLASSES__ classes', 'classes.id = student.class_id')
                ->where('student.grade_id', $grade_id)
                ->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('student.name|student.number', 'like', "%$keywords%");
                    }
                })
                ->where('student.school_id', $this->auth->school_id)
                ->where('student.status', \app\common\model\Student::DISPLAY)
                ->field('student.id,student.number,student.card_number,student.name,student.avatar,student.pinyin,student.gender,student.birthday,student.school_id,student.grade_id,student.class_id,student.created_at,classes.name class_name')
                ->paginate();
        }

        if ($list) {
            $list = $list->toArray();

            foreach ($list['data'] as $key => $datum) {
                if (!strstr($datum['avatar'], 'http') && !strpos($datum['avatar'], 'https')) {
                    if (strstr($datum['avatar'], '/uploads/')) {
                        $list['data'][$key]['avatar'] = config('app_url') . $datum['avatar'];
                    }
                }
            }
        }

        $this->success('', $list);

    }

    public function search()
    {
        $school_id = $this->request->request('school_id', 0);
        $keywords = $this->request->request('keywords', '');
        $page = $this->request->post('page', 1);

        if ($page) {
            $list = \app\common\model\Student::alias('student')
                ->join('__CLASSES__ classes', 'classes.id = student.class_id')
                ->join('__GRADE__ grade', 'grade.id = student.grade_id')
                ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
                ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
                ->where('student.school_id', $school_id)
                ->whereLike('student.name|student.number', "%$keywords%")
                ->field('student.*,classes.name class_name,grade.name grade_name,classes.headmaster headmaster_id,user.nickname headmaster_name')
                ->field('student.*,classes.name class_name,grade.name grade_name,classes.headmaster headmaster_id,user.nickname headmaster_name')
                ->paginate();

            if ($list) {
                $list = $list->toArray();

                foreach ($list['data'] as $key => $datum) {
                    if (!strstr($datum['avatar'], 'http') && strstr($datum['avatar'], '/uploads/')) {
                        $list['data'][$key]['avatar'] = config('app_url') . $datum['avatar'];
                    }
                }
            }
        } else {
            $list = \app\common\model\Student::alias('student')
                ->join('__CLASSES__ classes', 'classes.id = student.class_id')
                ->join('__GRADE__ grade', 'grade.id = student.grade_id')
                ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
                ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
                ->where('student.school_id', $school_id)
                ->whereLike('student.name|student.number', "%$keywords%")
                ->field('student.*,classes.name class_name,grade.name grade_name,classes.headmaster headmaster_id,user.nickname headmaster_name')
                ->column('student.*,classes.name class_name,grade.name grade_name,classes.headmaster headmaster_id,user.nickname headmaster_name', 'student.id');
            foreach ($list as $key => $value) {
                if (!strstr($value['avatar'], 'http') && strstr($value['avatar'], '/uploads/')) {
                    $list[$key]['avatar'] = config('app_url') . $value['avatar'];
                }
            }
        }
        $this->success('', $list);
    }

}
